﻿/*******************************************************************
 *     JameE
 *******************************************************************
  Copyright 2010 Clifford Champion and JameE contributors.
  
  This file is part of JameE.
  
  JameE is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
  
  JameE is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with JameE.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************/

 
/// <reference path="~/Base/Classer.js" />
/// <reference path="~/Base/IncludeLoader.js" />

$include(
[
	"Engine/ISceneObject.js"
],
function () {

	$class(
	"JameE.Engine.SceneObject: ISceneObject",
	{
		/////////////////////
		// FIELDS
		/////////////
		_strId: null,
		_oPosition: null,
		_oDimensions: null,
		_oAabb: null,
		_oScene: null,


		/////////////////////
		// CONSTRUCTORS
		/////////////
		_ctor: function () {
			// store id
			this._strId = $argument("id", "string") || null;

			// store position
			this._oPosition = $argument("position", "Vector3") || $vector3(0, 0);

			// store/calculate dimensions
			var dimensions = $argument("dimensions", "Vector3");
			var size = $argument("size", "number");

			if (dimensions !== null) {
				this._oDimensions = dimensions;
			}
			else if (size !== null) {
				this._oDimensions = $vector3(size, size);
			}
			else {
				this._oDimensions = null;
			}

			// update aabb
			this._calculateAabb();

		},


		/////////////////////
		// METHODS: ISceneObject
		/////////////
		message: function (oMessage) {
		},

		update: function (fDeltaTime, fTotalTime) {
		},

		draw: function (oContext) {
		},

		getAabb: function () {
			return this._oAabb;
		},

		getId: function () {
			return this._strId;
		},

		addedToScene: function (oScene) {
			this._oScene = oScene;
		},

		removedFromScene: function (oScene) {
			this._oScene = null;
		},


		/////////////////////
		// METHODS
		/////////////
		setPosition: function (fX, fY) {
			/// <summary>Sets the location of the object.</summary>
			$require_type(fX, "number");
			$require_type(fY, "number");

			this._oPosition.fX = x;
			this._oPosition.fY = y;

			this._calculateAabb();
		},

		getPosition: function () {
			return this._oPosition;
		},

		getPositionZ: function () {
			return this._oPosition.fZ;
		},

		_setDimensions: function (fWidth, fHeight) {
			/// <summary>Sets the width and height of this object.</summary>
			$require_type(fWidth, "number");
			$require_type(fHeight, "number");

			if (this._oDimensions === null) {
				this._oDimensions = new JameE.Math.Vector3();
			}

			this._oDimensions.fX = fWidth;
			this._oDimensions.fY = fHeight;

			this._calculateAabb();
		},

		_calculateAabb: function () {
			var fX = this._oPosition.fX;
			var fY = this._oPosition.fY;
			var fHalfWidth;
			var fHalfHeight;
			if (this._oDimensions !== null) {
				fHalfWidth = this._oDimensions.fX / 2.0;
				fHalfHeight = this._oDimensions.fY / 2.0;
			}
			else {
				fHalfWidth = 0;
				fHalfHeight = 0;
			}
			this._oAabb = new JameE.Engine.Aabb(
				{
					xMin: fX - fHalfWidth,
					xMax: fX + fHalfWidth,
					yMin: fY - fHalfHeight,
					yMax: fY + fHalfHeight,
					object: this
				}
			);
		}

	});

	$include_notify("Engine/SceneObject.js");

});


